package com.baitengsoft.patentservicemonitor.job.model;

import com.baitengsoft.patentservicemonitor.common.utils.DateUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangze
 * @date 2018-01-31 14:41.
 **/
public class PatentUpdateSearchParam {

    private QueryTypeEnum queryType;

    private String queryStr;

    private String batch;

    private String callbackUrl;

    private String folder;


    public PatentUpdateSearchParam(QueryTypeEnum queryType, String queryStr, String batch, String callbackUrl, String folder) {
        this.queryType = queryType;
        this.queryStr = queryStr;
        this.batch = batch;
        this.callbackUrl = callbackUrl;
        this.folder = folder;
    }

    public PatentUpdateSearchParam(Date date, QueryTypeEnum queryType, String queryStr, String callbackUrl, String folder) {
        this.queryType = queryType;
        this.queryStr = queryStr;
        this.batch = DateUtils.format(date, "yyyyMM");
        this.callbackUrl = callbackUrl;
        this.folder = String.format("%s/%s/%s", folder, DateUtils.getYear(date), DateUtils.getMonth(date));

    }


    public String getQueryStr() {
        return queryStr;
    }

    public void setQueryStr(String queryStr) {
        this.queryStr = queryStr;
    }

    public String getBatch() {
        return batch;
    }

    public void setBatch(String batch) {
        this.batch = batch;
    }

    public String getCallbackUrl() {
        return callbackUrl;
    }

    public void setCallbackUrl(String callbackUrl) {
        this.callbackUrl = callbackUrl;
    }

    public String getFolder() {
        return folder;
    }

    private void setFolder(String folder) {
        this.folder = folder;
    }

    public QueryTypeEnum getQueryType() {
        return queryType;
    }

    public void setQueryType(QueryTypeEnum queryType) {
        this.queryType = queryType;
    }


    public String getUrlParam() {
        Map<String, String> searchParams = new HashMap<>();
        searchParams.put("query_type", this.getQueryType().value);//查询类型 cdn/pa/an 区域 权利人 申请号
        searchParams.put("query", this.getQueryStr());
        searchParams.put("batch", this.getBatch());
        searchParams.put("fields", "an,ti,class,ls1_new,ad,apd,cagc,in_,pa,cpa1,caa,paa,sed,aic1,pd,ivd,tfd,ic1,ppa1,aa,cdn,pdn");
        searchParams.put("call_back", this.getCallbackUrl());
        searchParams.put("folder", this.getFolder());
        StringBuffer buffer = new StringBuffer();
        for (String key : searchParams.keySet()) {
            buffer.append(String.format("&%s=%s", key, searchParams.get(key)));
        }
        return buffer.toString().substring(1);
    }


    public enum QueryTypeEnum {
        CDN("区域", "cdn"), PA("权利人", "pa"), AN("申请号", "an");
        private String name;
        private String value;

        QueryTypeEnum(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }


        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}
